package com.api.gbhg.daiyanlu.action;

import com.api.gbhg.daiyanlu.Dao.RecordSubjectsDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 编制流程记录对应的功能科目
 * Mr.dyl
 * 2021.4.14
 */
public class RecordSubjects extends BaseBean implements Action {
    private static final Logger log = LoggerFactory.getLogger("gbhg");
    RecordSubjectsDao dao = new RecordSubjectsDao();

    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("===================各项目开始同步到功能科目表=====================");
        String tableGxmdygnkm = "uf_gxmdygnkm";
        String uuid = UUID.randomUUID().toString();
        String modeId = RecordSubjectsDao.getModeId(tableGxmdygnkm);//获取建模表Id
        String currentDateString = TimeUtil.getCurrentDateString();//获取当前日期
        String currentTimeString = TimeUtil.getOnlyCurrentTimeString();//获取当前时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);//当前年份
        int year1 = calendar.get(Calendar.YEAR) - 1;//上年年份
        String requestId = requestInfo.getRequestid();//请求requestId
       // dao.deleteOne(tableGxmdygnkm, requestId);//删除已有数据
        RecordSet rs = new RecordSet();
        String sql = "delete from " + tableGxmdygnkm + " where xmlcid = " + requestId + "";
        rs.execute(sql);

        String workflowid = dao.getRequestIdDao(requestId);//获取workflowid
        String formid = dao.getIdDao(workflowid);//获取formid
        String tablename = dao.getFromId(formid);//获取表名
        String id = dao.tableId(requestId, tablename);//获取流程id
        RecordSet lastYear = new RecordSet();//上年结转核批
        RecordSet thoseYear = new RecordSet();//当年预算核批
        //02流程特殊处理
        String workflowid02 = getPropValue("FormationWorkflowid", "BZ02Workflowid");//读取配置文件
        String activeworkflowid02 = WorkflowVersion.getActiveVersionWFID(workflowid02);
        //05流程特殊处理
        String workflowid05 = getPropValue("FormationWorkflowid", "BZ05Workflowid");//读取配置文件
        String activeworkflowid05 = WorkflowVersion.getActiveVersionWFID(workflowid05);
        //18流程特殊处理
        String workflowid18 = getPropValue("FormationWorkflowid", "BZ18Workflowid");//读取配置文件
        String activeworkflowid18 = WorkflowVersion.getActiveVersionWFID(workflowid18);
        //19流程特殊处理
        String workflowid19 = getPropValue("FormationWorkflowid", "BZ19Workflowid");//读取配置文件
        String activeworkflowid19 = WorkflowVersion.getActiveVersionWFID(workflowid19);
        //02和19流程 明细表 dt_5 dt_6
        log.info("读取配置文件:" + workflowid02 + "," + workflowid05 + "," + workflowid18 + "," + workflowid19);
        log.info("获取的workflowid:" + activeworkflowid02 + "," + activeworkflowid05 + "," + activeworkflowid18 + "," + activeworkflowid19);
        if (workflowid.equals(activeworkflowid02) || workflowid.equals(activeworkflowid19)) {
            // 上年结转核批明细
            lastYear = dao.lastYear2(id, tablename);
            // 当年预算核批明细
            thoseYear = dao.thoseYear2(id, tablename);
        } else if (workflowid.equals(activeworkflowid05)) {
            // 上年结转核批明细
            lastYear = dao.lastYear5(id, tablename);
            // 当年预算核批明细
            thoseYear = dao.thoseYear5(id, tablename);
        } else if (workflowid.equals(activeworkflowid18)) {
            // 上年结转核批明细
            lastYear = dao.lastYear18(id, tablename);
            // 当年预算核批明细
            thoseYear = dao.thoseYear18(id, tablename);
        } else {
            // 上年结转核批明细
            lastYear = dao.lastYear(id, tablename);
            // 当年预算核批明细
            thoseYear = dao.thoseYear(id, tablename);
        }

        /**
         * 上年结转核批
         */
        log.info("============================上年结转核批======================================");
        while (lastYear.next()) {
            String zyczbksr = " ";//上年结转核批字段
            int sum1 = 0;
            int sum2 = 0;
            String yj = getPropValue("Verification", "yj");//科目级
            String xj = getPropValue("Verification", "xj");//科目下级
            log.info("（科目下级）读取配置文件:" + yj);
            log.info("（科目级）读取配置文件:" + xj);
            String dt3 = " ";//上年结转核批0中央，1非中央
            zyczbksr = lastYear.getString("lastYear");
            log.info("上年结转核批字段：" + zyczbksr);
            if (!zyczbksr.equals("")) {
                String[] sta = zyczbksr.split("_");//上年结转核批下划线分割
                sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                sum2 = Integer.parseInt(sta[1]);//表id
            }
            //0为中央，1为非中央
            dt3 = lastYear.getString("dt3");//上年
            String z3 = lastYear.getString("z3");//上年结转项目支出
            log.info("中央非中央区分(上年结转)：" + dt3);
            log.info("上年结转项目支出：" + z3);
            //0为中央
            if (dt3.equals("0")) {
                //科目下级
                if (sum1 == Integer.parseInt(xj)) {
                    String subjectsSubordinate1 = dao.subjectsSubordinate1(sum2);
                    int subjectsSubordinate = dao.subjectsSubordinate(sum2);//上年结转核批
                    log.info("szgnkm:" + subjectsSubordinate);
                    int subjects = dao.subjects(subjectsSubordinate);//上年结转核批
                    log.info("gnkm:" + subjects);
                    RecordSet functionSubjects = dao.functionSubjects(subjects);//上年结转核批
                    Map<String, String> hashMap = new HashMap<>();
                    boolean a = false;
                    while (functionSubjects.next()) {
                        hashMap.put("xmlcid", requestId);
                        hashMap.put("dygnkmmc", subjectsSubordinate1);
                        hashMap.put("kmdm", functionSubjects.getStringNoTrim("kmdm"));
                        hashMap.put("dygnkmbid", functionSubjects.getStringNoTrim("id"));
                        hashMap.put("zfqd", "1");
                        hashMap.put("lb", "0");
                        hashMap.put("modedatacreater", "1");
                        hashMap.put("modedatacreatedate", currentDateString);
                        hashMap.put("modedatacreatertype", "0");
                        hashMap.put("modedatacreatetime", currentTimeString);
                        hashMap.put("formmodeid", modeId);
                        hashMap.put("modeuuid", uuid);
                        hashMap.put("kmjxj", "1");
                        hashMap.put("hpssjid", zyczbksr);
                        a = dao.insertBudgeLines(hashMap);
                    }
                    if (!a) {
                        requestInfo.getRequestManager().setMessagecontent("中央上年科目下级各项目同步到功能科目表失败");
                        requestInfo.getRequestManager().setMessageid("");
                        log.info("============================中央上年科目下级各项目同步到功能科目表失败======================================");
                        return FAILURE_AND_CONTINUE;
                    }
                }
            }
            //科目级
            if (sum1 == Integer.parseInt(yj)) {
                int subjects = dao.subjects(sum2);//上年结转核批
                log.info("(上年)gnkm:" + subjects);
                RecordSet functionSubjects = dao.functionSubjects(subjects);//上年结转核批
                Map<String, String> hashMap = new HashMap<>();
                boolean a = false;
                while (functionSubjects.next()) {
                    hashMap.put("xmlcid", requestId);
                    hashMap.put("dygnkmmc", functionSubjects.getStringNoTrim("kmmc"));
                    hashMap.put("kmdm", functionSubjects.getStringNoTrim("kmdm"));
                    hashMap.put("dygnkmbid", functionSubjects.getStringNoTrim("id"));
                    hashMap.put("zfqd", "1");
                    hashMap.put("lb", "0");
                    hashMap.put("modedatacreater", "1");
                    hashMap.put("modedatacreatedate", currentDateString);
                    hashMap.put("modedatacreatertype", "0");
                    hashMap.put("modedatacreatetime", currentTimeString);
                    hashMap.put("formmodeid", modeId);
                    hashMap.put("modeuuid", uuid);
                    hashMap.put("kmjxj", "0");
                    hashMap.put("hpssjid", zyczbksr);
                    a = dao.insertBudgeLines(hashMap);
                }
                if (!a) {
                    requestInfo.getRequestManager().setMessagecontent("中央上年科目级各项目同步到功能科目表失败");
                    requestInfo.getRequestManager().setMessageid("");
                    log.info("============================中央上年科目级各项目同步到功能科目表失败======================================");
                    return FAILURE_AND_CONTINUE;
                }
            }

            //1为非中央
            if (dt3.equals("1")) {
                String gnkm = " ";
                String classify = " ";
                if (!z3.equals("")) {
                    String szfl = dao.table1(z3);
                    gnkm = dao.table2(szfl);
                    classify = dao.table3(szfl);
                } else {
                    classify = dao.sfwdlhsdw(lastYear.getInt("zcqd"), year, lastYear.getInt("sbdw"));
                    gnkm = dao.theCentral1(lastYear.getInt("zcqd"), year, lastYear.getInt("sbdw"));
                }
                if (!gnkm.equals("")) {
                    RecordSet theCentral2 = dao.theCentral2(gnkm, year);
                    Map<String, String> hashMap = new HashMap<>();
                    boolean a=false;
                    while (theCentral2.next()) {
                        hashMap.put("xmlcid", requestId);
                        hashMap.put("fczbkxm", theCentral2.getStringNoTrim("kmmc"));
                        hashMap.put("yssrfl", classify);
                        hashMap.put("hpssjid", theCentral2.getStringNoTrim("id"));
                        hashMap.put("nd", lastYear.getString("ysnd"));
                        hashMap.put("zfqd", "1");
                        if (!z3.equals("")) {
                            hashMap.put("jb", "1");
                        } else {
                            hashMap.put("jb", "0");
                        }
                        hashMap.put("modedatacreater", "1");
                        hashMap.put("modedatacreatedate", currentDateString);
                        hashMap.put("modedatacreatertype", "0");
                        hashMap.put("modedatacreatetime", currentTimeString);
                        hashMap.put("formmodeid", modeId);
                        hashMap.put("modeuuid", uuid);
                        a = dao.insertBudgeLines1(hashMap);
                    }
                    if (!a) {
                        if(!z3.equals("")) {
                            requestInfo.getRequestManager().setMessagecontent("非中央上年项目同步到各流程核批非财政拨款表失败");
                            requestInfo.getRequestManager().setMessageid("");
                            log.info("============================非中央上年项目同步到各流程核批非财政拨款表失败======================================");
                            return FAILURE_AND_CONTINUE;
                        }else {
                            requestInfo.getRequestManager().setMessagecontent("非中央上年类别同步到各流程核批非财政拨款表失败");
                            requestInfo.getRequestManager().setMessageid("");
                            log.info("============================非中央上年类别同步到各流程核批非财政拨款表失败======================================");
                            return FAILURE_AND_CONTINUE;
                        }
                    }
                }
            }
        }

        /**
         * 当年预算核批
         */
        log.info("============================当年预算核批======================================");
        while (thoseYear.next()) {
            String zyczbksr1 = " ";//当年结转核批
            int sum3 = 0;
            int sum4 = 0;
            String yj = getPropValue("Verification", "yj");//科目下级
            String xj = getPropValue("Verification", "xj");//科目级
            log.info("（科目下级）读取配置文件:" + yj);
            log.info("（科目级）读取配置文件:" + xj);
            String dt4 = " ";//当年结转核批0中央，1非中央
            zyczbksr1 = thoseYear.getString("thoseYear");
            log.info("当年结转核批：" + zyczbksr1);
            if (!zyczbksr1.equals("")) {
                String[] sta1 = zyczbksr1.split("_");//当年结转核批下划线分割
                sum3 = Integer.parseInt(sta1[0]);//9为科目级，10为科目下级
                sum4 = Integer.parseInt(sta1[1]);//表id
                log.info("9为科目级，10为科目下级:" + sum3);
                log.info("表id:" + sum4);
            }
            dt4 = thoseYear.getString("dt4");//当年
            String z4 = thoseYear.getString("z4");//当年结转项目支出
            log.info("当年:" + dt4);
            log.info("当年结转项目支出:" + z4);
            //0为中央
            if (dt4.equals("0")) {
                //科目下级
                if (sum3 == Integer.parseInt(xj)) {
                    String subjectsSubordinate1 = dao.subjectsSubordinate1(sum4);
                    int subjectsSubordinate = dao.subjectsSubordinate(sum4);//当年结转核批
                    log.info("szgnkm:" + subjectsSubordinate);
                    int subjects = dao.subjects(subjectsSubordinate);//当年结转核批
                    log.info("gnkm:" + subjects);
                    RecordSet functionSubjects = dao.functionSubjects2(subjects);//当年结转核批
                    Map<String, String> hashMap = new HashMap<>();
                    boolean a = false;
                    while (functionSubjects.next()) {
                        hashMap.put("xmlcid", requestId);
                        hashMap.put("dygnkmmc", subjectsSubordinate1);
                        hashMap.put("kmdm", functionSubjects.getStringNoTrim("kmdm"));
                        hashMap.put("dygnkmbid", functionSubjects.getStringNoTrim("id"));
                        hashMap.put("zfqd", "0");
                        hashMap.put("lb", "0");
                        hashMap.put("modedatacreater", "1");
                        hashMap.put("modedatacreatedate", currentDateString);
                        hashMap.put("modedatacreatertype", "0");
                        hashMap.put("modedatacreatetime", currentTimeString);
                        hashMap.put("formmodeid", modeId);
                        hashMap.put("modeuuid", uuid);
                        hashMap.put("kmjxj", "1");
                        hashMap.put("hpssjid", zyczbksr1);
                        a = dao.insertBudgeLines(hashMap);
                    }
                    if (!a) {
                        requestInfo.getRequestManager().setMessagecontent("中央当年科目下级各项目同步到功能科目表失败");
                        requestInfo.getRequestManager().setMessageid("");
                        log.info("============================中央当年科目下级各项目同步到功能科目表失败======================================");
                        return FAILURE_AND_CONTINUE;
                    }
                }
            }
            //科目级
            if (sum3 == Integer.parseInt(yj)) {
                int subjects = dao.subjects(sum4);//当年结转核批
                log.info("(当年)gnkm:" + subjects);
                RecordSet functionSubjects = dao.functionSubjects2(subjects);//当年结转核批
                Map<String, String> hashMap = new HashMap<>();
                boolean a = false;
                while (functionSubjects.next()) {
                    hashMap.put("xmlcid", requestId);
                    hashMap.put("dygnkmmc", functionSubjects.getStringNoTrim("kmmc"));
                    hashMap.put("kmdm", functionSubjects.getStringNoTrim("kmdm"));
                    hashMap.put("dygnkmbid", functionSubjects.getStringNoTrim("id"));
                    hashMap.put("zfqd", "0");
                    hashMap.put("lb", "0");
                    hashMap.put("modedatacreater", "1");
                    hashMap.put("modedatacreatedate", currentDateString);
                    hashMap.put("modedatacreatertype", "0");
                    hashMap.put("modedatacreatetime", currentTimeString);
                    hashMap.put("formmodeid", modeId);
                    hashMap.put("modeuuid", uuid);
                    hashMap.put("kmjxj", "0");
                    hashMap.put("hpssjid", zyczbksr1);
                    a = dao.insertBudgeLines(hashMap);
                }
                if (!a) {
                    requestInfo.getRequestManager().setMessagecontent("中央当年科目级各项目同步到功能科目表失败");
                    requestInfo.getRequestManager().setMessageid("");
                    log.info("============================中央当年科目级各项目同步到功能科目表失败======================================");
                    return FAILURE_AND_CONTINUE;
                }
            }

            //1为非中央
            if (dt4.equals("1")) {
                String gnkm = " ";
                String classify = " ";
                if (!z4.equals("")) {
                    String szfl = dao.table1(z4);
                    gnkm = dao.table2(szfl);
                    classify = dao.table3(szfl);
                } else {
                    classify = dao.theCentral2(lastYear.getInt("zcqd"), year, lastYear.getInt("sbdw"));
                    gnkm = dao.theCentral1(lastYear.getInt("zcqd"), year, lastYear.getInt("sbdw"));
                }
                if (!gnkm.equals("")) {
                    RecordSet theCentral2 = dao.theCentral3(gnkm,year);
                    Map<String, String> hashMap = new HashMap<>();
                    boolean a=false;
                    while (theCentral2.next()) {
                        hashMap.put("xmlcid", requestId);
                        hashMap.put("fczbkxm", theCentral2.getStringNoTrim("kmmc"));
                        hashMap.put("yssrfl", classify);
                        hashMap.put("hpssjid", theCentral2.getStringNoTrim("id"));
                        hashMap.put("nd", lastYear.getString("ysnd"));
                        hashMap.put("zfqd", "1");
                        if (!z4.equals("")) {
                            hashMap.put("jb", "1");
                        } else {
                            hashMap.put("jb", "0");
                        }
                        hashMap.put("modedatacreater", "1");
                        hashMap.put("modedatacreatedate", currentDateString);
                        hashMap.put("modedatacreatertype", "0");
                        hashMap.put("modedatacreatetime", currentTimeString);
                        hashMap.put("formmodeid", modeId);
                        hashMap.put("modeuuid", uuid);
                        a = dao.insertBudgeLines1(hashMap);
                    }
                    if (!a) {
                        if(!z4.equals("")) {
                            requestInfo.getRequestManager().setMessagecontent("非中央当年项目同步到各流程核批非财政拨款表失败");
                            requestInfo.getRequestManager().setMessageid("");
                            log.info("============================非中央当年项目同步到各流程核批非财政拨款表失败======================================");
                            return FAILURE_AND_CONTINUE;
                        }else {
                            requestInfo.getRequestManager().setMessagecontent("非中央当年类别同步到各流程核批非财政拨款表失败");
                            requestInfo.getRequestManager().setMessageid("");
                            log.info("============================非中央当年类别同步到各流程核批非财政拨款表失败======================================");
                            return FAILURE_AND_CONTINUE;
                        }
                    }
                }
            }
        }
        return SUCCESS;
    }
}
